using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;


/// <summary>
/// Summary description for ServidorPedido
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ServidorPedido : System.Web.Services.WebService {
    public static System.Collections.SortedList lstPedidos = new SortedList();

    public ServidorPedido () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 


    }

    [WebMethod]
    public string Comprar(String Item, String MT) {

        Cola clsCola = new Cola();
        clsCola.AgregarPedido(this.GetHashCode(), DateTime.Parse(MT));

        //Llamada Interna
        ServidorCatalogo clsServidorCatalogo = new ServidorCatalogo(true);
        
        int Stock=0;
        string Price="";
        Boolean HayItem = false;
        System.Xml.XmlDocument Res = new System.Xml.XmlDocument();

        try
        {
            Res.LoadXml(clsServidorCatalogo.QueryByItem(Item,MT));

            foreach (System.Xml.XmlNode Nodo in Res["RESULT"].ChildNodes)
            {
                Stock = Int32.Parse(Nodo.Attributes["Stock"].Value);
                Price = Nodo.Attributes["Price"].Value;
                HayItem = true;
            }

            if (HayItem)
            {

                if (Stock > 0)
                {
                    Stock--;
                    clsServidorCatalogo.Update(Item, Stock.ToString(), Price, MT);
                    clsCola.RemoverItem();
                    lstPedidos.Add(lstPedidos.Count + 1, Item);
                    return ("Item comprado");
                }
                else
                {
                    clsCola.RemoverItem();
                    return ("No hay stock disponible");
                }
            }
            else
            {
                clsCola.RemoverItem();
                return ("No existe el libro");
            }
        }
        catch (Exception e)
        {
            clsCola.RemoverItem();
            return (e.Message);
        }
    }

    [WebMethod]
    public string ListaPedidos()
    {
        System.Xml.XmlDocument ResultXml = new System.Xml.XmlDocument();
        ResultXml.LoadXml("<RESULT/>");

        int j = 0;
        try
        {
            for (j = 0; j < lstPedidos.Count; j++)
            {
                System.Xml.XmlNode Nodo = ResultXml.CreateNode(System.Xml.XmlNodeType.Element, "Item", String.Empty);
                Nodo.Attributes.Append(ResultXml.CreateAttribute(string.Empty, "Title", string.Empty));
                Nodo.Attributes["Title"].Value = lstPedidos.GetByIndex(j).ToString();
                ResultXml.DocumentElement.AppendChild(Nodo);

            }

            return ResultXml.InnerXml;
        }
        catch (Exception e)
        {
            return (e.Message);
        }
    }
}

